import LoginPage from '../pages/LoginPage';
import AdminPage from '../pages/admin/AdminPage';
import DashboardPage from '../pages/admin/DashboardPage';
import SystemPage from '../pages/admin/SystemPage';
import { Route, Redirect } from 'react-router-dom';

export const routeList = [
    {
        path: '/login',
        component: LoginPage,
    },
    {
        path: '/admin',
        component: AdminPage,
    },
];

// 函数式组件
export const RouterView = ({ routes }) => {
    return routes.map((item) => {
        console.log('item', item);
        return (
            <Route
                key={item.path}
                path={item.path}
                render={(props) => {
                    // 从本地存储中拿登录态
                    const token = window.sessionStorage.getItem('token');

                    // 没有，去登录
                    if (!token) {
                        return (
                            <Redirect
                                to={{
                                    pathname: '/login',
                                    state: {
                                        redirect: props.location.pathname,
                                    },
                                }}
                            />
                        );
                    }

                    console.log(item);

                    return <item.component {...props} />;
                }}
            />
        );
    });
};
